//处理路径拼接
export const handlerPathJoin = (paths) => {
  let str = "";
  paths.forEach((item) => {
    const url = item.trim();
    if (url) {
      if (url.startsWith("/")) {
        str += url;
      } else {
        str += `/${url}`;
      }
    }
  });
  return str;
};

//处理api返回图片路径
export const handlerPicPath = (pic, key = "pic") => {
  // console.log(import.meta.env);
  if (typeof pic === "string") {
    const p = pic.replace(/\\/g, "/").replace("public", "");
    return import.meta.env.VITE_HOST_URL + p;
  } else if (Array.isArray(pic)) {
    return pic.map((item) => {
      const res = { ...item };
      const p = res[key].replace(/\\/g, "/").replace("public", "");
      res[key] = import.meta.env.VITE_HOST_URL + p;
      return res;
    });
  }
  return pic;
};

//分解网址
export const parseURL = (urlString) => {
  const url = new URL(urlString);
  return {
    protocol: url.protocol,
    host: url.host,
    hostname: url.hostname,
    port: url.port,
    pathname: url.pathname,
    search: url.search,
    hash: url.hash,
  };
};
//获取后缀名
export const getExtension = (url, str = ".") => {
  const arr = url.split(".");
  const a = arr.slice(0, -1).join(".");
  const b = "." + arr[arr.length - 1];
  return [a, b];
};
